package joyi.train.service.impl;

import j.util.pkgenerator.PG;
import joyi.train.cheng.domain.City;
import j.data.tree.TreeData;
import j.data.tree.TreeNode;
import j.data.tree.TreeSetting;
import j.util.jdbc.RowMapper;
import joyi.train.service.CityService;
import j.framework.core.ServiceContext;
import j.util.jdbc.JdbcTemplate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WY2015092804 on 2015/10/12.
 */
public class CityServiceImpl implements CityService {

    @Override
    public Map<String, Object> getCity(ServiceContext serviceContext) {
        Map<String, Object> map = new HashMap<>();
        JdbcTemplate jdbcTemplate = serviceContext.getJdbcTemplate();
        List<Map<String, Object>> cityList = jdbcTemplate.queryForListMap("SELECT cityId, cityName, parentId _parentId, addDateTime, addUserId, leftValue, rightValue FROM      cheng_City");
        map.put("rows", cityList);
        map.put("total", cityList.size());
        return map;
    }

    @Override
    public Map<String, Object> delCity(ServiceContext serviceContext) {

        String cityId = serviceContext.params.getString("id");
        JdbcTemplate jdbcTemplate = serviceContext.getJdbcTemplate();
        int a = jdbcTemplate.queryForInt("select count(*) from cheng_City where parentId=?", cityId);
        if (a > 0) {
            serviceContext.actionResult.add("data", "有下属地区");
            return null;
        } else if (jdbcTemplate.queryForInt("select count(*) from cheng_City_Customer where cityId = ?", cityId) > 0) {
            serviceContext.actionResult.add("data", "此地区有客户");
            return null;
        } else {
            City city = new City();
            city.setCityId(cityId);
            int row = jdbcTemplate.delete(city);
            Map<String, Object> map = new HashMap<>();
            if (row == 0) {
                map.put("da", "删除的地区对象已经不存在");
            } else {
                map.put("da", "删除成功");
            }
            return map;
        }

    }

    @Override
    public void edit(ServiceContext serviceContext) {
        City city = serviceContext.getBean(City.class);
        serviceContext.getJdbcTemplate().update(city);
    }

    @Override
    public void add(ServiceContext serviceContext) {
        City city = serviceContext.getBean(City.class);
        city.setCityId(PG.getNext("CIT"));
        city.setAddedTime(new Date());
        city.setAddedUserId(serviceContext.getUser().getUserId());
        serviceContext.getJdbcTemplate().insert(city);
    }

    @Override
    public TreeData getTree(ServiceContext serviceContext) {
        JdbcTemplate jt = serviceContext.getJdbcTemplate();
        List<City> list;
        list = jt.queryForList("select * from cheng_city",
                new RowMapper<City>() {
                    @Override
                    public City mapRow(ResultSet rs, int rowNum) throws SQLException {
                        City city = new City();
                        city.setCityId(rs.getString("cityId"));
                        city.setCityName(rs.getString("cityName"));
                        city.setParentId(rs.getString("parentId"));
                        return city;
                    }
                });
        TreeSetting ts = new TreeSetting(false, false);
        ts.setNameFieldName("text");
        TreeData td = new TreeData(ts);
        TreeNode node;
        for (City city : list) {
            node = new TreeNode(city.getCityId(), city.getCityName());
            node.setpId(city.getParentId());
            td.addNode(node);
        }
        return td;
    }

    @Override
    public City getCityById(ServiceContext serviceContext) {
        String cityId = serviceContext.getDesId("cityId");
        City city = (City)serviceContext.getJdbcTemplate().queryForObject("select * from cheng_City where cityId='"+cityId+"'");
        return city;
    }

    @Override
    public City doShowEditCity(ServiceContext serviceContext) {
        String cityId = serviceContext.params.getString("id");
        City c = serviceContext.getJdbcTemplate().queryForDomain("select * from cheng_City where cityId = ?", new RowMapper<City>() {
            @Override
            public City mapRow(ResultSet rs, int rowNum) throws SQLException {
                City city = new City();
                city.setCityId(rs.getString("cityId"));
                city.setCityName(rs.getString("cityName"));
                city.setParentId(rs.getString("parentId"));
                city.setNote(rs.getString("note"));
                return city;
            }
        }, cityId);
        return c;
    }
}
